/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.nodes;

import com.googlecode.ivybeans.facade.ArtifactInfo;
import java.beans.PropertyChangeListener;
import javax.swing.Icon;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.spi.java.project.support.ui.PackageView;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;

class ArtifactNode extends AbstractNode {

    private ArtifactInfo artifactInfo;
    private final static String LIBRARIES_ICON = "org/netbeans/modules/java/j2seproject/ui/resources/libraries.gif";
    
    
    public ArtifactNode(ArtifactInfo artifactInfo) {
        super(new JarContentFilterChildren(PackageView.createPackageView(new ArtifactSourceGroup(artifactInfo))));
        this.artifactInfo = artifactInfo;
        setIconBaseWithExtension(LIBRARIES_ICON);
    }

    @Override
    public String getDisplayName() {
        return artifactInfo.getName()+" "+ artifactInfo.getVersion();
    }

    @Override
    public String getName() {
        return artifactInfo.getName()+" "+ artifactInfo.getVersion();
    }

    
    private static class ArtifactSourceGroup implements SourceGroup {
        
        private ArtifactInfo artInfo;
        
        ArtifactSourceGroup(ArtifactInfo artInfo) {
            this.artInfo = artInfo;
        }
        
        public void addPropertyChangeListener(PropertyChangeListener listener) {
        }

        public boolean contains(FileObject file) throws IllegalArgumentException {
            return true;
        }

        public String getDisplayName() {
            return artInfo.getName();
        }

        public Icon getIcon(boolean opened) {
            return null;
        }

        public String getName() {
            return artInfo.getName();
        }

        public FileObject getRootFolder() {
            FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(artInfo.getJarFile()));
            if (fo != null) {
                return FileUtil.getArchiveRoot(fo);
            }
            
            return null;
        }

        public void removePropertyChangeListener(PropertyChangeListener listener) {
        }
        
    }
    
     private static class JarContentFilterChildren extends FilterNode.Children {

        JarContentFilterChildren(Node orig) {
            super(orig);
        }

        @Override
        protected Node copyNode(Node node) {
            return new JarFilterNode(node);
        }
    }

    private static class JarFilterNode extends FilterNode {

        JarFilterNode(Node original) {
            super(original, new JarContentFilterChildren(original));
        }
    }
        
}
